package com.ninthgroup.service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ninthgroup.entity.User;
import com.ninthgroup.mapper.UserMapper;
import com.ninthgroup.service.UserService;
import com.ninthgroup.vo.Result;
import com.ninthgroup.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author ： 1452863884@qq.com rcg
 * @date ：Created in 2022/10/13 20:50
 * @description：用户接口的实现类
 * @modified By：
 * @version:1.0
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;
    @Override
    public Result findByCondition(Integer currentPage, Integer pageSize, UserVo userVo) {
        PageHelper.startPage(currentPage,pageSize);
        List<User> users = userMapper.selectAll(userVo.getUsername(), userVo.getRealname());
        PageInfo<User> pageInfo=new PageInfo<>(users);

        return new Result(2000,"查询成功",pageInfo);

    }

    @Override
    public Result deleteUserId(Integer userId) {
        int i = userMapper.deleteByPrimaryKey(userId);
        return new Result(200,"删除成功");
    }

    @Override
    public Result save(User user) {
        int i = userMapper.insertSelective(user);
        return new Result(200,"添加成功");
    }

    @Override
    public Result update(User user) {

        if(user.getuId()==null){
            User user1 = userMapper.slectByUserName(user.getUsername());
            user.setuId(user1.getuId());
            userMapper.updateByPrimaryKeySelective(user);
        }else{
          userMapper.updateByPrimaryKeySelective(user);
        }

        return new Result(200,"修改成功");
    }

    @Override
    public Result select(String username, String realname) {
        List<User> users = userMapper.selectAll(username, realname);
        return new Result(200,"查询成功",users);
    }

    @Override
    public int add(User user) {
        int i = userMapper.insertSelective(user);
        return i;
    }

    @Override
    public User findnameandpwd(User user) {
        return userMapper.findByUsernameAndPassword(user.getUsername(),user.getPassword());
    }

    @Override
    public Result findOneById(Integer uId) {
        User user = userMapper.selectByPrimaryKey(uId);
        return new Result(200,"修改个人信息",user) ;
    }
}